<template>
  <view class="demand-list-page">
    <!-- 顶部导航栏 -->
    <view class="page-header">
      <view class="header-inner">
        <view class="back-btn" @click="handleBack">
          <u-icon name="arrow-left" color="#1f2937" size="18"></u-icon>
        </view>
        <text class="header-title">热门需求</text>
        <view class="search-btn" @click="handleSearch">
          <u-icon name="search" color="#1f2937" size="18"></u-icon>
        </view>
      </view>
    </view>

    <!-- 筛选栏 -->
    <view class="filter-bar">
      <scroll-view scroll-x class="filter-scroll" :show-scrollbar="false">
        <view class="filter-list">
          <view 
            v-for="(category, index) in filterCategories" 
            :key="index"
            :class="['filter-item', { 'active': selectedCategory === category.value }]"
            @click="handleCategoryChange(category.value)"
          >
            <text class="filter-text">{{ category.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 需求列表 -->
    <scroll-view scroll-y class="content-scroll">
      <view class="demand-list">
        <view 
          v-for="demand in filteredDemands" 
          :key="demand.id"
          class="demand-item"
          @click="handleDemandClick(demand)"
        >
          <view class="demand-card">
            <!-- 顶部彩色横条 -->
            <view :class="['top-bar', getTopBarClass(demand.tag)]"></view>
            
            <!-- 卡片内容 -->
            <view class="card-content">
              <!-- 标题和标签 -->
              <view class="card-header">
                <text class="demand-title">{{ demand.title }}</text>
                <view :class="['demand-tag', getTagClass(demand.tag)]">
                  <text class="tag-text">{{ demand.tag }}</text>
                </view>
              </view>

              <!-- 描述 -->
              <text class="demand-desc">{{ demand.desc }}</text>

              <!-- 位置、浏览量和预算 -->
              <view class="demand-meta">
                <view class="meta-left">
                  <view class="meta-item">
                    <u-icon name="map" size="10" color="#6b7280"></u-icon>
                    <text class="meta-text">{{ demand.location }}</text>
                  </view>
                  <view class="meta-item">
                    <u-icon name="eye" size="10" color="#6b7280"></u-icon>
                    <text class="meta-text">{{ demand.views }}</text>
                  </view>
                </view>
                <text class="demand-budget">{{ demand.budget }}</text>
              </view>

              <!-- 用户信息和联系按钮 -->
              <view class="card-footer">
                <view class="user-info">
                  <view class="info-item">
                    <u-icon name="account" size="10" color="#6b7280"></u-icon>
                    <text class="info-text">{{ demand.user }}</text>
                  </view>
                  <view class="info-item">
                    <u-icon name="phone" size="10" color="#6b7280"></u-icon>
                    <text class="info-text">{{ demand.phone }}</text>
                  </view>
                  <view class="info-item">
                    <u-icon name="clock" size="10" color="#6b7280"></u-icon>
                    <text class="info-text">{{ demand.time }}</text>
                  </view>
                </view>
                <view class="contact-btn" @click.stop="handleContact(demand)">
                  <text class="btn-text">联系我</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 底部安全区 -->
      <view class="safe-bottom"></view>
    </scroll-view>

    <!-- 发布弹窗 -->
    <PublishModal :show="showPublishModal" @close="showPublishModal = false" />
    
    <!-- 底部导航栏 -->
    <TabBar :current="3" @publish="showPublishModal = true" />
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { demandCategories, tagColorMap, topBarColorMap } from '@/utils/constants'
import PublishModal from '@/components/yjyny/PublishModal.vue'
import TabBar from '@/components/yjyny/TabBar.vue'

// 筛选分类
const filterCategories = [
  { value: '', label: '全部' },
  ...demandCategories
]

// 选中的分类
const selectedCategory = ref('')

// 发布弹窗
const showPublishModal = ref(false)

// 需求数据
const demandsData = ref([
  {
    id: 1,
    title: '急需100亩有机蔬菜种植地',
    desc: '寻找适合有机蔬菜种植的土地，要求土壤肥沃，灌溉条件良好，交通便利',
    tag: '种植合作',
    location: '浙江杭州',
    budget: '20万',
    views: '328',
    user: '陈**',
    phone: '188****1234',
    time: '2小时前'
  },
  {
    id: 2,
    title: '寻找大型收割机服务',
    desc: '需要专业的收割机团队，约200亩水稻收割，要求设备先进，经验丰富',
    tag: '农机租赁',
    location: '江苏南京',
    budget: '5万',
    views: '456',
    user: '黄**',
    phone: '139****5678',
    time: '5小时前'
  },
  {
    id: 3,
    title: '农业技术培训咨询',
    desc: '需要专业的农业技术指导，包括病虫害防治、施肥技术等方面的培训',
    tag: '技术咨询',
    location: '安徽合肥',
    budget: '面议',
    views: '892',
    user: '刘**',
    phone: '156****8765',
    time: '1天前'
  },
  {
    id: 4,
    title: '寻找农产品销售渠道',
    desc: '有大量优质苹果待售，寻找可靠的销售渠道和合作伙伴',
    tag: '产品销售',
    location: '山东烟台',
    budget: '约50吨',
    views: '1.2k',
    user: '杨**',
    phone: '182****2345',
    time: '1天前'
  },
  {
    id: 5,
    title: '农资集采服务需求',
    desc: '需要大批量农资采购服务，包括化肥、种子、农药等，希望价格优惠',
    tag: '集采代购',
    location: '河南郑州',
    budget: '预算30万',
    views: '678',
    user: '吴**',
    phone: '151****3456',
    time: '2天前'
  }
])

// 过滤后的需求列表
const filteredDemands = computed(() => {
  if (!selectedCategory.value) {
    return demandsData.value
  }
  return demandsData.value.filter(demand => demand.tag === selectedCategory.value)
})

// 获取标签样式类
const getTagClass = (tag) => {
  const colorKey = tagColorMap[tag] || 'primary'
  return `tag-${colorKey}`
}

// 获取顶部横条样式类
const getTopBarClass = (tag) => {
  const colorKey = topBarColorMap[tag] || 'primary'
  return `bar-${colorKey}`
}

// 事件处理
const handleBack = () => {
  uni.navigateBack()
}

const handleSearch = () => {
  uni.navigateTo({ url: '/pages/yjyny/search/index' })
}

const handleCategoryChange = (value) => {
  selectedCategory.value = value
}

const handleDemandClick = (demand) => {
  uni.navigateTo({ url: `/pages/yjyny/demand/detail?id=${demand.id}` })
}

const handleContact = (demand) => {
  uni.showModal({
    title: '联系方式',
    content: `联系人：${demand.user}\n电话：${demand.phone}`,
    showCancel: true,
    cancelText: '取消',
    confirmText: '拨打电话',
    success: (res) => {
      if (res.confirm) {
        uni.makePhoneCall({
          phoneNumber: demand.phone.replace(/\*/g, '5')
        })
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.demand-list-page {
  min-height: 100vh;
  background-color: #f9fafb;
}

// 顶部导航栏
.page-header {
  background-color: #ffffff;
  position: sticky;
  top: 0;
  z-index: 10;
  border-bottom: 2rpx solid #f3f4f6;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.header-inner {
  display: flex;
  align-items: center;
  padding: 24rpx 32rpx;
  gap: 24rpx;
}

.back-btn,
.search-btn {
  width: 64rpx;
  height: 64rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
}

.back-btn:active,
.search-btn:active {
  background-color: #f3f4f6;
}

.header-title {
  flex: 1;
  font-size: 32rpx;
  font-weight: bold;
  color: #1f2937;
  text-align: center;
}

// 筛选栏
.filter-bar {
  background-color: #ffffff;
  border-bottom: 2rpx solid #f3f4f6;
  padding: 24rpx 0;
}

.filter-scroll {
  width: 100%;
  white-space: nowrap;
}

.filter-list {
  display: inline-flex;
  gap: 16rpx;
  padding: 0 32rpx;
}

.filter-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12rpx 32rpx;
  border-radius: 48rpx;
  background-color: #f3f4f6;
  transition: all 0.3s;
  flex-shrink: 0;
  
  &.active {
    background-color: #10b981;
    
    .filter-text {
      color: #ffffff;
      font-weight: 500;
    }
  }
}

.filter-text {
  font-size: 24rpx;
  color: #6b7280;
  white-space: nowrap;
}

// 内容滚动区
.content-scroll {
  height: calc(100vh - 200rpx);
}

// 需求列表
.demand-list {
  padding: 24rpx 32rpx;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.demand-item {
  width: 100%;
}

.demand-card {
  background-color: #ffffff;
  border-radius: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  border: 2rpx solid #f3f4f6;
  overflow: hidden;
  transition: all 0.3s;
}

.demand-card:active {
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

// 顶部彩色横条
.top-bar {
  height: 4rpx;
  
  &.bar-primary {
    background-color: #10b981;
  }
  
  &.bar-secondary {
    background-color: #3b82f6;
  }
  
  &.bar-accent {
    background-color: #f59e0b;
  }
}

// 卡片内容
.card-content {
  padding: 32rpx;
}

// 标题和标签
.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.demand-title {
  flex: 1;
  font-size: 28rpx;
  font-weight: bold;
  color: #1f2937;
  padding-right: 16rpx;
  transition: color 0.3s;
}

.demand-tag {
  padding: 8rpx 16rpx;
  border-radius: 8rpx;
  font-size: 22rpx;
  flex-shrink: 0;
  
  &.tag-primary {
    background-color: rgba(16, 185, 129, 0.1);
    color: #10b981;
  }
  
  &.tag-secondary {
    background-color: rgba(59, 130, 246, 0.1);
    color: #3b82f6;
  }
  
  &.tag-accent {
    background-color: rgba(245, 158, 11, 0.1);
    color: #f59e0b;
  }
}

.tag-text {
  font-size: 22rpx;
}

// 描述
.demand-desc {
  font-size: 24rpx;
  color: #6b7280;
  line-height: 1.5;
  margin-bottom: 24rpx;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

// 位置、浏览量和预算
.demand-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24rpx;
  padding-bottom: 24rpx;
}

.meta-left {
  display: flex;
  align-items: center;
  gap: 24rpx;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8rpx;
}

.meta-text {
  font-size: 22rpx;
  color: #6b7280;
}

.demand-budget {
  font-size: 28rpx;
  font-weight: bold;
  color: #f59e0b;
}

// 用户信息和联系按钮
.card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16rpx;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 24rpx;
  flex-wrap: wrap;
  flex: 1;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8rpx;
}

.info-text {
  font-size: 22rpx;
  color: #6b7280;
}

.contact-btn {
  padding: 16rpx 32rpx;
  background-color: #10b981;
  border-radius: 16rpx;
  transition: opacity 0.3s;
  flex-shrink: 0;
}

.contact-btn:active {
  opacity: 0.8;
}

.btn-text {
  font-size: 24rpx;
  color: #ffffff;
}

// 底部安全区
.safe-bottom {
  height: 160rpx;
}
</style>
